package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author: Miyako
 * @Date: 2024-07-30-14:09
 * @Description: 实现基本修改密码功能，并通过控制判断是否成功
 */
public class JDBCDemo02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306" +
                "/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia" +
                "/Shanghai&rewriteBatchedStatements=true","root","root");

        Statement statement = connection.createStatement();
        Scanner sc = new Scanner(System.in );
        System.out.println("输入你要修改密码的用户名");
        String username = sc.nextLine();
        System.out.println("输入你修改后的密码");
        String newPassword = sc.nextLine();

        String sql = "UPDATE userinfo Set password='" +newPassword+"'"+"where username = '"+username+"'";

        int i =statement.executeUpdate(sql);

        if (i==0){
            System.out.println("修改失败，没有这条数据");
        }else {
            System.out.println("修改成功！");
        }
    }
}
